import {Module} from "../framework.js"
import * as WidgetEvent from "../widget/widget-event.js"
import * as UI from "../ui.js"

export default class WidgetTools extends Module {
    constructor(disp) {
        super(disp);
        this._ui = UI.UIWidgetTool();
        this._WID = undefined;
        this._arrElement = [];

        this.Subscribe([WidgetEvent.EVT_WIDGET_CHANGED_TO_VIEW, WidgetEvent.EVT_WIDGET_CHANGED_TO_EDIT_BLUR], event => {
            if(event.WID === this._WID) {
                this._Hide();
            }
        });
        this.Subscribe(WidgetEvent.EVT_WIDGET_CHANGED_TO_EDIT_FOCUS, event => {
            this._Show(event.WID, event.menuElements);
        });
        /*
        this.Subscribe(WidgetEvent.EVT_WIDGET_CONTENT_CHANGED, event => {
            if(event.WID === this._WID) {
                this._widgetContentChanged = true;
            }
        });
        */

        this._ui.SetAction(id => {
            console.log("widget tool");
            if(!this._WID) return;

            switch(id) {
                case "tool_up":
                    this.Dispatch(WidgetEvent.MoveUpEvent(this._WID));
                    break;
                case "tool_down":
                    this.Dispatch(WidgetEvent.MoveDownEvent(this._WID));
                    break;
                case "tool_focus":
                    this.Dispatch(WidgetEvent.ToolFocusEvent(this._WID));
                    break;
                case "tool_delete":
                    this.Dispatch(WidgetEvent.ToolDeleteEvent(this._WID));
                    break;
                case "tool_close":
                    const _WID = this._WID;
                    this._Hide();
                    this.Dispatch(WidgetEvent.ToolCloseEvent(_WID));
                    break;
            }
        });
    }

    _Show(WID, menuElements) {
        this._WID = WID;
        /*
        menuElements.forEach((elem) => {
            this._arrElement.push(elem);
        });
        */
        this._ui.Show();
    }

    Hide(WID, force = false) {
        if(force) this._Hide();
        else
            if(WID === this._WID) {
                this._Hide();
            }
    }

    _Hide() {
        this._WID = null;
        /*
        this._arrElement.forEach((elem) => {
            elem.remove();
        });
        */
        this._arrElement.length = 0;
        this._ui.Hide();
    }
}

